package com.huajx.core.udp.impl;

import com.huajx.core.udp.UDPClient;
import com.huajx.core.udp.config.UDPClientConfig;
import com.huajx.util.StrUtil;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.nio.NioDatagramConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;

/**
 * UDP client 基本实现类
 *
 * 参见：https://blog.csdn.net/polo_longsan/article/details/50094879
 * 
 * @author: lisa 叶新华
 * @create: 2019-09-26 14:09
 */
public abstract class BaseUdpClient implements UDPClient {
    public static final int DEFAULT_PORT = 60000;
    public static final String DEFAULT_IP = "192.168.1.3";

    private static final Logger logger = LoggerFactory.getLogger(BaseUdpClient.class);
    private boolean isRunning;

    private int port;
    private String ip;
    private int sn;
    private String name;
    private String remark;

    private InetSocketAddress inetSocketAddress;

    private IoSession session;

    private IoConnector connector;

    protected abstract IoHandlerAdapter getHandler();

    /**
     * 编码处理工厂类
     *
     * @return
     */
    protected abstract ProtocolCodecFilter getProtocolCodecFilter();

    public void start(String ip, int port) {
        synchronized (this) {
//            if (isRunning) {
//                logger.warn("服务器已经启动！");
//                return;
//            }
//            if (port < 80) {
//                port = DEFAULT_PORT;
//            }
//
//            setInetSocketAddress(new InetSocketAddress(ip, port));
//            setConnector(new NioDatagramConnector());
//
//            connector.setHandler(getHandler());
//            connector.getFilterChain().addLast("codec", getProtocolCodecFilter());
//            ConnectFuture connectFuture = connector.connect(getInetSocketAddress());
//
//            // 等待是否连接成功，相当于是转异步执行为同步执行。
//            connectFuture.awaitUninterruptibly();
        }
    }

    /**
     * 停止监听HTTP服务
     */
    @Override
    public void stop() {
    }

    @Override
    public boolean isRunning() {
        return isRunning;
    }

    @Override
    public void config(UDPClientConfig config) {
        int port = config.getPort();
        this.port = port <= 80 ? DEFAULT_PORT : port;
        this.name = config.getName();
        this.remark = config.getRemark();
        this.sn = config.getSn();
        String ip = config.getIp();
        this.ip = StrUtil.isBlank(ip) ? DEFAULT_IP : ip;
//        setEncoding(StrUtil.isBlank(config.getEncoding()) ? CharsetUtil.UTF_8 : config.getEncoding());
    }

    public InetSocketAddress getInetSocketAddress() {
        return inetSocketAddress;
    }

    public void setInetSocketAddress(InetSocketAddress inetSocketAddress) {
        this.inetSocketAddress = inetSocketAddress;
    }

    public IoSession getSession() {
        return session;
    }

    public void setSession(IoSession session) {
        this.session = session;
    }

    public IoConnector getConnector() {
        return connector;
    }

    public void setConnector(IoConnector connector) {
        this.connector = connector;
    }
}
